import Settings from '../config/Settings.js';

export default class Carrot {
  constructor(game, options = {}) {
    this.game = game;
    
    this.x = options.x || game.canvas.width;
    this.y = options.y || 0;
    
    // 设置胡萝卜类型
    this.type = options.type || this.getRandomType();
    const typeConfig = Settings.carrot.types[this.type];
    
    // 根据类型设置大小
    this.width = Settings.carrot.width * typeConfig.scale;
    this.height = Settings.carrot.height * typeConfig.scale;
    
    this.image = game.getImage('carrot');
    this.collected = false;
    
    // 动画相关
    this.floatOffset = 0;
    this.floatSpeed = 0.05;
    this.rotationAngle = 0;
    
    // 特效相关
    this.glowAlpha = 0;
    this.glowDirection = 1;
  }
  
  getRandomType() {
    const types = Object.entries(Settings.carrot.types);
    const totalChance = types.reduce((sum, [_, config]) => sum + config.chance, 0);
    let random = Math.random() * totalChance;
    
    for (const [type, config] of types) {
      random -= config.chance;
      if (random <= 0) return type;
    }
    
    return 'normal';
  }
  
  update() {
    // 胡萝卜向左移动
    this.x -= Settings.platform.speed;
    
    // 更新浮动效果
    this.floatOffset = Math.sin(this.game.gameTime * this.floatSpeed) * 5;
    
    // 更新发光效果
    this.glowAlpha += 0.02 * this.glowDirection;
    if (this.glowAlpha >= 1) {
      this.glowDirection = -1;
    } else if (this.glowAlpha <= 0.3) {
      this.glowDirection = 1;
    }
    
    // 特殊类型的胡萝卜会缓慢旋转
    if (this.type !== 'normal') {
      this.rotationAngle += 0.02;
    }
  }
  
  render(ctx) {
    if (this.collected) return;
    
    ctx.save();
    
    // 应用浮动和旋转效果
    ctx.translate(this.x + this.width/2, this.y + this.height/2 + this.floatOffset);
    if (this.type !== 'normal') {
      ctx.rotate(this.rotationAngle);
    }
    
    // 绘制发光效果
    const typeConfig = Settings.carrot.types[this.type];
    if (this.type !== 'normal') {
      const gradient = ctx.createRadialGradient(0, 0, 0, 0, 0, this.width);
      gradient.addColorStop(0, typeConfig.color);
      gradient.addColorStop(1, 'rgba(255,255,255,0)');
      ctx.globalAlpha = this.glowAlpha;
      ctx.fillStyle = gradient;
      ctx.fillRect(-this.width, -this.height, this.width * 2, this.height * 2);
      ctx.globalAlpha = 1;
    }
    
    try {
      if (this.image && !this.image.isPlaceholder) {
        // 根据类型着色
        ctx.fillStyle = typeConfig.color;
        ctx.globalAlpha = 0.3;
        ctx.fillRect(-this.width/2, -this.height/2, this.width, this.height);
        ctx.globalAlpha = 1;
        
        // 绘制胡萝卜图片
        ctx.drawImage(
          this.image,
          -this.width/2,
          -this.height/2,
          this.width,
          this.height
        );
      } else {
        this.drawCarrot(ctx);
      }
    } catch (error) {
      console.error('渲染胡萝卜时发生错误:', error);
      this.drawCarrot(ctx);
    }
    
    ctx.restore();
  }
  
  drawCarrot(ctx) {
    const typeConfig = Settings.carrot.types[this.type];
    
    // 绘制基础形状
    ctx.fillStyle = typeConfig.color;
    ctx.beginPath();
    ctx.moveTo(-this.width/2, -this.height/2);
    ctx.lineTo(this.width/2, -this.height/2);
    ctx.lineTo(0, this.height/2);
    ctx.closePath();
    ctx.fill();
    
    // 绘制叶子
    ctx.fillStyle = '#4CAF50';
    ctx.fillRect(-this.width/4, -this.height/2 - 10, 10, 10);
    ctx.fillRect(this.width/4 - 10, -this.height/2 - 10, 10, 10);
  }
  
  checkCollision(player) {
    if (this.collected) return false;
    
    // 如果玩家有磁铁效果，增加收集范围
    let collectionRange = 0;
    if (player.hasMagnet) {
      collectionRange = player.magnetRange || 0;
    }
    
    return (
      this.x - collectionRange < player.x + player.width/2 &&
      this.x + this.width + collectionRange > player.x - player.width/2 &&
      this.y - collectionRange < player.y + player.height &&
      this.y + this.height + collectionRange > player.y
    );
  }
  
  collect() {
    if (this.collected) return 0;
    
    this.collected = true;
    const typeConfig = Settings.carrot.types[this.type];
    
    // 创建收集特效
    this.game.particleSystem.createParticles(
      this.x + this.width/2,
      this.y + this.height/2,
      typeConfig.color,
      10,
      5,
      5,
      30
    );
    
    // 应用特殊效果
    if (typeConfig.effects) {
      if (typeConfig.effects.points) {
        // 添加双倍积分效果
        this.game.player.pointsMultiplier = typeConfig.effects.points;
        setTimeout(() => {
          this.game.player.pointsMultiplier = 1;
        }, typeConfig.effects.duration * (1000/60));
      }
      
      if (typeConfig.effects.shield) {
        // 添加护盾效果
        this.game.player.hasShield = true;
        setTimeout(() => {
          this.game.player.hasShield = false;
        }, typeConfig.effects.duration * (1000/60));
      }
      
      if (typeConfig.effects.magnet) {
        // 添加磁铁效果
        this.game.player.hasMagnet = true;
        this.game.player.magnetRange = typeConfig.effects.range;
        setTimeout(() => {
          this.game.player.hasMagnet = false;
          this.game.player.magnetRange = 0;
        }, typeConfig.effects.duration * (1000/60));
      }
    }
    
    // 显示得分提示
    this.game.floatingTexts.push({
      content: `+${typeConfig.score}`,
      x: this.x,
      y: this.y - 30,
      color: typeConfig.color,
      alpha: 1,
      velocity: -2
    });
    
    return typeConfig.score;
  }
  
  // 检查是否在屏幕外
  isOffScreen() {
    return this.x + this.width < 0;
  }
  
  // 静态方法：创建胡萝卜阵型
  static createPattern(game, pattern, x, y) {
    const carrots = [];
    const patternConfig = Settings.carrot.patterns[pattern];
    
    switch(pattern) {
      case 'line':
        for (let i = 0; i < patternConfig.count; i++) {
          carrots.push(new Carrot(game, {
            x: x + i * patternConfig.spacing,
            y: y
          }));
        }
        break;
        
      case 'triangle':
        for (let i = 0; i < patternConfig.count; i++) {
          carrots.push(new Carrot(game, {
            x: x + i * patternConfig.spacing.x,
            y: y + (i % 2) * patternConfig.spacing.y
          }));
        }
        break;
        
      case 'circle':
        for (let i = 0; i < patternConfig.count; i++) {
          const angle = (i / patternConfig.count) * Math.PI * 2;
          carrots.push(new Carrot(game, {
            x: x + Math.cos(angle) * patternConfig.radius,
            y: y + Math.sin(angle) * patternConfig.radius
          }));
        }
        break;
        
      default: // single
        carrots.push(new Carrot(game, { x, y }));
    }
    
    return carrots;
  }
} 